package com.boronstream.istar.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

import com.boronstream.istar.R;
import com.boronstream.istar.R.anim;
import com.boronstream.istar.R.drawable;
import com.boronstream.istar.model.Horoscope;

public class HelperUtils {
	private static final String TAG = HelperUtils.class.getSimpleName();
	private static HelperUtils mHelperUtils;

	public static HelperUtils getInstance() {

		if (mHelperUtils == null) {
			mHelperUtils = new HelperUtils();
		}
		return mHelperUtils;
	}
	/**
	 * Get picture resource by name
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public int getDrawableIdByName(Context context, String name) {
		drawable d = new drawable();
		int id = -1;
		Field[] fields = R.drawable.class.getDeclaredFields();
		for (Field field : fields) {
			if (field.getName().equals(name)) {
				try {
					id = field.getInt(d);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
		return id;
	}
	
	/**
	 * Get picture resource by name
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public int getAnimdByName(Context context, String name) {
		anim d = new anim();
		int id = -1;
		Field[] fields = R.anim.class.getDeclaredFields();
		for (Field field : fields) {
			if (field.getName().equals(name)) {
				try {
					id = field.getInt(d);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
		return id;
	}
	
	/**
	 * Save cache datas for hot tag.
	 * @param context
	 * @param tags
	 */
	public void saveHoroscope(Context context, Horoscope horoscope,String prefName) {
		SharedPreferences sharedPref = context.getSharedPreferences(
				context.getString(R.string.app_name), Context.MODE_PRIVATE);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(horoscope);

			Editor editor = sharedPref.edit();
			String myBase64 = new String(Base64.encode(baos.toByteArray(),
					Base64.DEFAULT));
			editor.putString(prefName, myBase64);
			editor.commit();
			oos.close();
			baos.close();
		} catch (IOException e1) {
			Logger.w(TAG, "" + e1.getMessage());
		}
	}

	/**
	 * get cache datas for hot tags.
	 * @param context
	 * @return
	 */
	public Horoscope getHoroscope(Context context,String prefName) {
		try {
			SharedPreferences sharedPref = context.getSharedPreferences(
					context.getString(R.string.app_name), Context.MODE_PRIVATE);
			String myBase64 = sharedPref.getString(prefName, "");
			byte[] myBytes = Base64.decode(myBase64.getBytes(), Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(myBytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			Horoscope horoscope= (Horoscope) ois.readObject();

			ois.close();
			bais.close();
			return horoscope;
		} catch (Exception e) {
			Logger.w(TAG, "" + e.getMessage());
		}
		return null;
	}
}
